package com.sam.mvp.ui.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.sam.R;
import com.sam.api.EvenBusTag;
import com.sam.di.component.DaggerMainComponent;
import com.sam.di.module.MainModule;
import com.sam.mvp.entity.HomeEnity;
import com.sam.mvp.entity.User;
import com.sam.mvp.contract.MainContract;
import com.sam.mvp.presenter.MainPresenter;
import com.sam.mvp.ui.adapter.MainAdapter;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.progressmanager.ProgressListener;
import me.jessyan.progressmanager.ProgressManager;
import me.jessyan.progressmanager.body.ProgressInfo;
import timber.log.Timber;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View, DefaultAdapter.OnRecyclerViewItemClickListener, ProgressListener {

    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    @Inject
    MainAdapter mAdapter;
    @Inject
    RecyclerView.LayoutManager mLayoutManager;



    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMainComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .mainModule(new MainModule(this))
                .build()
                .inject(this);


    }

    String url = "https://new.diaigame.com/apk/20170816/com.cqdjb.yg13_PM000177_.apk";

    @OnClick(R.id.btn_get_data)
    void getUserData() {
        mPresenter.getUserData(true);
    }

    @OnClick(R.id.btn_go_module)
    void goModule() {
        ARouter.getInstance().build("/home/main").navigation();
    }

    //    void goModule() {
//        ARouter.getInstance().build("/samModule/main").navigation();
//    }
    @OnClick(R.id.btn_go_download)
    void goDownLoad() {
        EventBus.getDefault().post("xxx", EvenBusTag.GOTO_EVENTBUS);
        mPresenter.downLoadImg(url);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        ProgressManager.getInstance().addResponseListener(url, this);
        ArmsUtils.configRecyclerView(mRecyclerView, mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @SuppressLint("NewApi")
    @Override
    public void showTextContent(List users) {
        StringBuilder sb = new StringBuilder();
        ((List<User>) users).stream().parallel().forEach(user ->
                sb.append("\nname: ")
                        .append(user.login)
                        .append("\n").append("url: ")
                        .append(user.avatar_url)
        );
        etContent.setText(sb.toString());
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onItemClick(View view, int viewType, Object data, int position) {
        Timber.tag("SamTag").i("login : %s %d", ((User) data).login, position);
        ARouter.getInstance().build("/samModule/main").navigation();

    }

    @Override
    public void onProgress(ProgressInfo progressInfo) {
        int progress = progressInfo.getPercent();
        Timber.tag("SamTag").i("progress: %s", progress);
    }

    @Override
    public void onError(long id, Exception e) {

    }
}
